package com.dms.bo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class IOUtil {
	
	@SuppressWarnings("resource")
	public static List<String> loadLogRec(File file) throws Exception{
		
		List<String> recList = new ArrayList<String>();
		BufferedReader br=null;
			br = new BufferedReader(
					new InputStreamReader(
							new FileInputStream(file),"utf-8"));
			String recData=null;
			while((recData=br.readLine())!=null){
				recList.add(recData);
			}
		return recList;
		
	}
	
	

	public static void saveLong(long lon, File file) {
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
			pw.println(lon);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			pw.close();
		}

	}

	public static long readLong(File file) throws Exception {

		FileInputStream fis = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fis, "utf-8");
		BufferedReader br = new BufferedReader(isr);

		return Long.parseLong(br.readLine());

	}

	public static String readString(RandomAccessFile raf, int length) throws IOException {
		byte[] bytes = new byte[length];
		raf.read(bytes);
		return new String(bytes, "ISO8859-1");

	}

	public static void saveCollection(Collection c, File file) {

		PrintWriter pw = null;
		try {
			pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"), true);
			for (Object o : c) {
				pw.println(o);
			}
		} catch (Exception e) {

		} finally {
			pw.close();
		}

	}

	public static List<LogData> loadLogData(File file) throws Exception{
		List<LogData> list = new ArrayList<LogData>();
			BufferedReader br = new BufferedReader(
					new InputStreamReader(
							new FileInputStream(file), "utf-8"));
			String str = null;
			while ((str = br.readLine()) != null) {
				LogData data = new LogData(str);
				list.add(data);
			}
			return list;

	}
	
	
	
	

}
